package svcmng

import (
	"fmt"

	. "starsine.cn/appTpl/cmn/defs"
	"starsine.cn/appTpl/cmn/logs"
	. "starsine.cn/appTpl/svccenter/registry/svcmng/iservice"
)

var serviceMng *ServiceMng = nil

func Start(appCtxt *AppContext, rpcMode bool) error {
	serviceMng = CreateServiceMng(appCtxt, rpcMode)

	logs.Debugf("svcmng start")

	return nil
}

func ServiceReg(vType, vName, userSpace string, data SvcData) (svc IService, err error) {
	if serviceMng == nil {
		logs.Errorf("serviceMng not init")
		return nil, fmt.Errorf("serviceMng not init")
	}

	return serviceMng.ServiceReg(vType, vName, userSpace, data)
}

func ServiceUnreg(vType, vName, vAddr string) error {
	if serviceMng == nil {
		logs.Errorf("serviceMng not init")
		return fmt.Errorf("serviceMng not init")
	}

	serviceMng.ServiceUnreg(vType, vName, vAddr)

	return nil
}

func ServiceUnregByInstanceID(instanceID string) error {
	if serviceMng == nil {
		logs.Errorf("serviceMng not init")
		return fmt.Errorf("serviceMng not init")
	}

	return serviceMng.ServiceUnregByInstanceID(instanceID)
}

func DumpService() string {
	return serviceMng.DumpService()
}

func GetService(instanceID string) (IService, bool) {
	return serviceMng.getService(instanceID)
}

func GetServiceList() []IService {
	return serviceMng.GetServiceList()
}

func ForEach(f func(k string, v IService) bool) {
	serviceMng.forEach(f)
}

func GetServiceListByType(vType string) []IService {
	return serviceMng.GetServiceListByType(vType)
}
